//
//  Extension+String.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/4/2.
//

import UIKit
import CommonCrypto
extension String{
    public typealias ID = Int
    public var id :ID{
        return hashValue
    }
    //去掉首尾的空格
    func stringByTrim() ->String{
        return self.trimmingCharacters(in: .whitespacesAndNewlines)
    }
    //字符串MD5加密
    var md5String: String {
        let str = self.cString(using: String.Encoding.utf8)
        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        CC_MD5(str!, strLen, result)

        let hash = NSMutableString()

        for i in 0..<digestLen {
            hash.appendFormat("%02x", result[i])
        }
        result.deallocate()
        return hash as String
    }
}
extension Int :Identifiable{
    public typealias ID = Int
    public var id :ID{
        return self
    }
}
extension Array :Identifiable where Element:Identifiable{
    public typealias ID = Int
    public var id :Int{
        var hasher = Hasher()
        forEach{ hasher.combine($0.id)}
        return hasher.finalize()
    }
}
